package com.mes.handler;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 获取到货单检验项目
 */
public class QualityFileResultHandler extends BaseActionHandler{
	private Logger logger = Logger.getLogger(this.getClass());
	@Override
	public JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		try {
			OBContext.setAdminMode();
			JSONObject reqData = new JSONObject(data);
			String id = reqData.getString("id");

			String sql="SELECT  " +
					"   xm.NAME AS xmname, " +
					"   cg.code as CGCODE , " +
					"   cg.wms_delivery_id as CGID ," +
					"   xm.code AS xmcode,  " +
					"   wl.m_product_id AS XMID,  " +
					"   wl.NAME AS wlname,  " +
					"   mx.qty AS QTY_TEST,  " +
					"   gys.NAME AS gysname,  " +
					"   gys.emes_vendor_id AS GYSID,  " +
					"   ck.NAME AS ckname,  " +
					"   ck.m_warehouse_id AS CKID,  " +
					"   kw.m_locator_id AS KWID,  " +
					"   dw.c_uom_id as DWID,   " +
					"   xm.emesq_inspection_item_id AS ITEMID,  " +
					"   0 AS QTY_ERROR,  " +
					"   mx.Wms_Delivery_Detail_id AS DETAILID,  " +
					"   0 AS QTY   " +
					"FROM  " +
					"   Wms_Delivery_Detail mx  " +
					"   LEFT JOIN wms_delivery cg ON cg.wms_delivery_id = mx.wms_delivery_id  " +
					"   LEFT JOIN Emesq_Quality_RR zj ON mx.m_product_id = zj.m_product_id  " +
					"   LEFT JOIN Emesq_Inspection_Group mxz ON zj.emesq_inspection_group_id = mxz.emesq_inspection_group_id  " +
					"   LEFT JOIN emesq_inspection_group_item item ON mxz.emesq_inspection_group_id = item.emesq_inspection_group_id  " +
					"   LEFT JOIN emesq_inspection_item xm ON item.emesq_inspection_item_id = xm.emesq_inspection_item_id  " +
					"   LEFT JOIN emes_vendor gys ON mx.emes_vendor_id = gys.emes_vendor_id  " +
					"   LEFT JOIN m_product wl ON mx.m_product_id = wl.m_product_id  " +
					"   LEFT JOIN m_warehouse ck ON cg.m_warehouse_id = ck.m_warehouse_id  " +
					"   LEFT JOIN m_locator kw ON mx.m_locator_id = kw.m_locator_id  " +
					"   left join c_uom dw on dw.c_uom_id = mx.c_uom_id     " +
					"   left join emes_color ys on mx.emes_color_id = ys.emes_color_id  " +
					"   left join emes_size cm on mx.emes_size_id = cm.emes_size_id   " +
					" WHERE  " +
					"   mx.Wms_Delivery_Detail_id = ? " +
					" and mx.status = 'CREATE'  ";
			Query query = OBDal.getInstance().getSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			query.setString(0, id);
			List<Map> datas = query.list();
			jsonRep.put("data", toJosnList(datas));
			jsonRep.put("result", true);
			jsonRep.put("msg", "获取数据成功");
		}catch (Exception e){
			OBDal.getInstance().rollbackAndClose();
			e.printStackTrace();
			logger.error("修改失败：", e);
		}
		return jsonRep;
	}

	private Collection toJosnList(List<Map> datas) {
		List<JSONObject> result = new ArrayList<JSONObject>();
		try {
			JSONObject obj = null;
			for (Map map : datas) {
				obj = new JSONObject();
				for (Object key : map.keySet()) {
					obj.put(((String)key).toUpperCase().trim(), map.get(key));
				}
				result.add(obj);
			}
		} catch (Exception e) {
		}
		return result;
	}
}


